package com.pansoft.openplanet.widget;

import android.app.Activity;
import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.pansoft.openplanet.adapter.AmountRechargeAdapter;
import com.pansoft.openplanet.bean.RechargeMethodBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cimu on 2018/9/17.
 * 选择积分卡充值方式
 */

public class RechargeMethodPop extends PopupWindow implements View.OnClickListener{
    private LinearLayout llSelectPayFor, llSelectWeChat;
    private Context mContext;
    private View mPopView;
    private RecyclerView rvMoneyType;
    private AmountRechargeAdapter mAmountRechargeAdapter;
    private ImageView ivPaySelect, ivPaySelectDown, ivChatSelect, ivChatSelectDown, ivRechargeClose;
    private RechargePhoto rechargePhoto;
    private List<RechargeMethodBean> mMethodBeans = new ArrayList<>();
    public RechargeMethodPop(Context context) {
        super(context);
        this.mContext = context;
        init(mContext);
        setPopupWindow();
    }
    public void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        // 绑定布局
        mPopView = inflater.inflate(R.layout.open_planet_pop_recharge_method, null);
        RechargeMethodBean payForBean = new RechargeMethodBean();
        payForBean.setBackGround(R.drawable.open_planet_recharge_pay_for);
        payForBean.setName(ResStringUtil.getString(R.string.open_planet_pay_for));
        payForBean.setSelectDown(R.drawable.open_planet_recharge_select_down);
        mMethodBeans.add(payForBean);
        RechargeMethodBean weChatBean = new RechargeMethodBean();
        weChatBean.setBackGround(R.drawable.open_planet_recharge_we_chat);
        weChatBean.setName(ResStringUtil.getString(R.string.open_planet_we_chat));
        weChatBean.setSelectDown(R.drawable.open_planet_recharge_select);
        mMethodBeans.add(weChatBean);
        ivRechargeClose = mPopView.findViewById(R.id.iv_recharge_close);
        ivRechargeClose.setOnClickListener(this);
        rvMoneyType = mPopView.findViewById(R.id.rv_money_type);
        mAmountRechargeAdapter = new AmountRechargeAdapter(context,mMethodBeans);
        LinearLayoutManager linearLayoutManager =new LinearLayoutManager(context);
        rvMoneyType.setLayoutManager(linearLayoutManager);
        rvMoneyType.setAdapter(mAmountRechargeAdapter);

        mAmountRechargeAdapter.setOnClickListener(new AmountRechargeAdapter.OnClickListener() {
            @Override
            public void onClick(int position) {
                rechargePhoto.getRechargePhoto(position);
                dismiss();
            }
        });

        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }

    public void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setContentView(mPopView);// 设置View
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
//        this.setHeight(Dev.dp2px(mContext, 234));// 设置弹出窗口的高
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setAnimationStyle(R.style.star_asset_pop_anim);
        backgroundAlpha(0.4f);

    }
    /**
     * 定义一个接口，公布出去 在Activity中操作
     */

    public void setRechargePhoto(RechargePhoto rechargePhoto) {
        this.rechargePhoto = rechargePhoto;
    }

    public interface RechargePhoto {
        void getRechargePhoto(int position);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.iv_recharge_close){
            dismiss();
        }

    }

    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = ((Activity)mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity)mContext).getWindow().setAttributes(lp);
    }

}
